package good

import (
	"api/pb/good"
	"context"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetGoodListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetGoodListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGoodListLogic {
	return &GetGoodListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetGoodListLogic) GetGoodList(req *types.GoodListRequest) (resp *types.GoodListResponse, err error) {
	goodResp, err := l.svcCtx.GoodRpc.GetGoodList(l.ctx, &good.ListRequest{Page: req.Page, PageSize: req.PerPage})
	if err != nil {
		return nil, err
	}

	resp = &types.GoodListResponse{
		Total: goodResp.Total,
		Pages: goodResp.Pages,
		Data:  make([]types.Good, 0),
	}
	for _, goodInfo := range goodResp.Data {
		resp.Data = append(resp.Data, types.Good{
			Id:    goodInfo.Id,
			Name:  goodInfo.Name,
			Price: goodInfo.Price,
			Stock: goodInfo.Stock,
		})
	}
	return resp, nil
}
